extern "C"
{
#include <stdlib.h>
}
#include "options.hpp"

namespace night_sky
{
  Options::Options(const int argc, const char* argv[])
  {
    std::vector<std::string> args(argc);
    for (int i=0; i<argc; ++i) {
      args.push_back(std::string(argv[i]));
    }
    parseToMap(args);
  }

  Options::Param Options::str2param(const std::string& str) const
  {
    if (str.empty()) {
      return Options::UNKNOWN;
    }
    if (str == "-p") {
      return Options::PORT;
    }
    return Options::UNKNOWN;
  }

  Options::~Options()
  {}

  void Options::parseToMap(const std::vector<std::string>& args)
  {
    typedef std::vector<std::string>::const_iterator citer;

    citer it = args.begin();
    while (it != args.end()) {
      const std::string key = *it;
      switch (str2param(key)) {
        case Options::PORT: {
            citer nit = ++it;
            if (nit != args.end()) {
              this->m_intMap.insert(
                  int_map::value_type("port", atoi(nit->c_str())));
            }
          }
          break;
        case Options::UNKNOWN:
          break;
      }
      ++it;
    }
  }

}// namespace night_sky
